//
//  XTCellOfxinge.m
//  TTLestening
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XTCellOfxinge.h"
#import "XCModelForHeader.h"
#import "XCModelForFind.h"
#import "XCCollectionCellForHeader.h"
#import "XCcollectionOfgedan.h"


#define WIDTH  self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface XTCellOfxinge ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)UICollectionView *collection;

@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, retain)XCModelForHeader *model;

@property (nonatomic, retain) NSArray *arrayForHotList;


@end
@implementation XTCellOfxinge


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       
        [self creatcollectioon];
        
        
    }
    
    return self;
}

-(void)creatcollectioon{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowLayout.minimumInteritemSpacing =5;
    self.flowLayout.minimumLineSpacing = 5;
    self.flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 40);

    
    
    //    self.collection = [[UICollectionView alloc]initWithFrame:self.contentView.frame collectionViewLayout:flowLayout];
    self.collection = [[UICollectionView alloc]initWithFrame:self.contentView.frame collectionViewLayout:self.flowLayout];
    [self.contentView addSubview:self.collection];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.pagingEnabled = YES;
    self.collection.bounces = NO;
    
    
    UINib *nibcell = [UINib nibWithNibName:@"XCcollectionOfgedan" bundle:[NSBundle mainBundle]];
    [self.collection registerNib:nibcell forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerNib:[UINib nibWithNibName:@"XCCollectionCellForHeader" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cellForSong"];

    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    XCCollectionCellForHeader *cell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cellForSong" forIndexPath:indexPath];
    
    [cell passModelFor:self.model];
    
    return cell;
    
    
}


- (void)passModelForArray:(NSArray *)array headerModel:(XCModelForHeader *)model {
    
    self.model = model;
    self.arrayForHotList = array;
    
    [self.collection reloadData];
    
    
}



-(void)layoutSubviews{
    
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 40)/3, HEIGHT  - 45 );
    self.collection.frame = self.contentView.frame;
   
    
    self.collection.contentInset = UIEdgeInsetsMake(0, 10 , 0, 10 );
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XCcollectionOfgedan *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    XCModelForFind *model = [self.arrayForHotList objectAtIndex:indexPath.row];
    
    [cell passModel:model];

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XCModelForFind *model = [self.arrayForHotList objectAtIndex:indexPath.row];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNewSong" object:nil userInfo:@{@"model": model.action}];
    

}


- (void)awakeFromNib {
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
